package com.bringway.p2p.controller;

import java.io.OutputStream;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.bringway.framework.controller.JsonController;
import com.bringway.framework.excel.ExcelOperUtil;
import com.bringway.framework.excel.ExcelSheet;
import com.bringway.framework.util.DateUtils;
import com.bringway.framework.util.RequestUtils;
import com.bringway.global.domain.SysUserDetails;
import com.bringway.p2p.service.TjrService;

/**
 * 
 * @author zhai
 * @version 创建时间：2015年12月14日 上午9:34:42
 * @功能：抽奖结果页面
 */


@Controller
@RequestMapping("/tjr")
public class TjrController extends JsonController{
	Logger logger = Logger.getLogger(PosController.class);
	@Autowired
	private TjrService tjrService;

	/**
	 * 
	 * @param request 推荐人信息查询
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/querytjr")
	public ModelAndView queryCjjgList(HttpServletRequest request,HttpServletResponse response) throws Exception{
		JSONObject result = new JSONObject();
		JSONObject params = new JSONObject();
		Integer page = RequestUtils.getIntParameter(request, "page");//新版本,传参
		Integer start = RequestUtils.getIntParameter(request, "start");
		Integer limit = RequestUtils.getIntParameter(request, "limit");
		if (null!=page) {//新版本,传参
			start=(page-1)*limit;
		}
		String queryType = RequestUtils.getStringParameter(request, "queryType");
		String id = RequestUtils.getStringParameter(request, "id");
		String sfzNum = RequestUtils.getStringParameter(request, "sfzNum");
		String realName = RequestUtils.getStringParameter(request, "realName");
		String user_name = RequestUtils.getStringParameter(request, "user_name");
		String sjhm = RequestUtils.getStringParameter(request, "sjhm");
		String tjr = RequestUtils.getStringParameter(request, "tjr");
		String regStartDate = RequestUtils.getStringParameter(request, "regStartDate");
		String regEndDate = RequestUtils.getStringParameter(request, "regEndDate");
		if(StringUtils.isNotEmpty(queryType) && "YWY_SSHK".equals(queryType)){//业务员所属客户查询
			SysUserDetails sysUserDetails =	(SysUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			tjr = sysUserDetails.getGyh();//业务员所属客户查询-当前登陆客户作为推荐人
		}
		
		params.put("start", start);
		params.put("limit", limit);
		params.put("id", id);
		params.put("sfzNum", sfzNum);
		params.put("realName", realName);
		params.put("user_name", user_name);
		params.put("sjhm", sjhm);
		params.put("tjr", tjr);
		params.put("regStartDate", regStartDate);
		params.put("regEndDate", regEndDate);
		
		List list = tjrService.querytjr(params);
		int count = tjrService.gettjrCount(params);
		result.put("data", list);	
		result.put("total", count);
		if (null!=page) {//新版本,传参
			int totalPage=count%limit==0?count/limit:count/limit+1;
			result.put("totalPage", totalPage);
		}
		serialize(request,response,result);
		return null;
	}
	

	
	/**
	 * @方法名称:updatetjr
	 * @方法描述:更新用户信息
	 * @author qjy
	 * @date 2015年12月29日下午1:55:30
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/updateTjr")
	public ModelAndView updateTjr(HttpServletRequest request, HttpServletResponse response) throws Exception {
		SysUserDetails sysUserDetails =	(SysUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

		String id = RequestUtils.getStringParameter(request, "id");
		String name = RequestUtils.getStringParameter(request, "name");
		String sjhm = RequestUtils.getStringParameter(request, "sjhm");
		String ytjr = RequestUtils.getStringParameter(request, "ytjr");
		String xtjr = RequestUtils.getStringParameter(request, "tjrx");
		JSONObject parms = new JSONObject();
		parms.put("id", id);
		parms.put("name", name);
		parms.put("sjhm", sjhm);
		parms.put("ytjr", ytjr);
		parms.put("xtjr", xtjr);
		parms.put("gyh", sysUserDetails.getGyh());
		JSONObject result = new JSONObject();
		boolean isCheckTjr=tjrService.isCheckTjr(name, xtjr);
		if (!isCheckTjr) {
			result.put("success", true);
			result.put("isCheckTjr", false);
			serialize(request, response, result);
			return null;
		}
		boolean bool = tjrService.updateTjr(parms);
		tjrService.updateTjjbByUsername(name);
		result.put("success", true);
		result.put("isUpdate", bool);
		serialize(request, response, result);
		return null;
	}

	/**
	 * @方法名称:updateTjrno
	 * @方法描述:更新用户信息不加条件限制
	 * @author qjy
	 * @date 2015年12月29日下午1:55:30
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/updateTjrno")
	public ModelAndView updateTjrno(HttpServletRequest request, HttpServletResponse response) throws Exception {
		SysUserDetails sysUserDetails =	(SysUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

		String id = RequestUtils.getStringParameter(request, "id");
		String name = RequestUtils.getStringParameter(request, "name");
		String sjhm = RequestUtils.getStringParameter(request, "sjhm");
		String ytjr = RequestUtils.getStringParameter(request, "ytjr");
		String xtjr = RequestUtils.getStringParameter(request, "tjrx");
		JSONObject parms = new JSONObject();
		parms.put("id", id);
		parms.put("name", name);
		parms.put("sjhm", sjhm);
		parms.put("ytjr", ytjr);
		parms.put("xtjr", xtjr);
		parms.put("gyh", sysUserDetails.getGyh());
		JSONObject result = new JSONObject();
		boolean bool = tjrService.updateTjr(parms);
		tjrService.updateTjjbByUsername(name);
		result.put("success", true);
		result.put("isUpdate", bool);
		serialize(request, response, result);
		return null;
	}

	
	/**
	 * @方法名称:add到新表
	 * @方法描述:添加数据
	 * @author qjy
	 * @date 2016年06月17日下午4:28:11
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/addTjr")
	public ModelAndView addTjr(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		JSONObject parms = new JSONObject();
		JSONObject result = new JSONObject();
		String id = RequestUtils.getStringParameter(request, "ID");
		String name = RequestUtils.getStringParameter(request, "USER_NAME");
		String sjhm = RequestUtils.getStringParameter(request, "SJHM");
		String ytjr = RequestUtils.getStringParameter(request, "OLD_TJR");
		String xtjr = RequestUtils.getStringParameter(request, "NEW_TJR");
		String xgtime = RequestUtils.getStringParameter(request, "BG_DATE");
	
		parms.put("ID", id);
		parms.put("USER_NAME", name);
		parms.put("SJHM", sjhm);
		parms.put("OLD_TJR", ytjr);
		parms.put("NEW_TJR", xtjr);
		parms.put("BG_DATE", xgtime);
		result.put("success", true);
		serialize(request, response, result);
		return null;
	}
	
	
	/**
	 * 查询最新推荐人信息
	 * @param request
	 * @param response
	 * @return  最新推荐人信息
	 * @throws Exception
	 */
	
	@RequestMapping("/queryxTjr")
	public ModelAndView queryxTjr(HttpServletRequest request,HttpServletResponse response) throws Exception{
		JSONObject result = new JSONObject();
		JSONObject params = new JSONObject();
		Integer start = RequestUtils.getIntParameter(request, "start");
		Integer limit = RequestUtils.getIntParameter(request, "limit");
		Integer page = RequestUtils.getIntParameter(request, "page");//新版本,传参
		if (null!=page) {//新版本,传参
			start=(page-1)*limit;
		}
		String name = RequestUtils.getStringParameter(request, "name");
		String sjhm = RequestUtils.getStringParameter(request, "sjhm");
		String ytjr = RequestUtils.getStringParameter(request, "ytjr");
		String xtjr = RequestUtils.getStringParameter(request, "xtjr");
		String xgtime = RequestUtils.getStringParameter(request, "xgtime");
		String regStartDate = RequestUtils.getStringParameter(request, "regStartDate");
		String regEndDate = RequestUtils.getStringParameter(request, "regEndDate");
		
		params.put("start", start);
		params.put("limit", limit);
		params.put("name", name);
		params.put("sjhm", sjhm);
		params.put("ytjr", ytjr);
		params.put("xtjr", xtjr);
		params.put("xgtime", xgtime);
		params.put("regStartDate", regStartDate);
		params.put("regEndDate", regEndDate);
		
		List list = tjrService.queryxTjr(params);
		int count = tjrService.queryxTjrcount(params);
		result.put("data", list);	
		result.put("total", count);
		if (null!=page) {//新版本,传参
			int totalPage=count%limit==0?count/limit:count/limit+1;
			result.put("totalPage", totalPage);
			
		}
		serialize(request,response,result);
		return null;
	}
	
	

	
	
	/**
	 * 导出推荐人信息
	 * @param request
	 * @param response
	 * @return  导出excel表格
	 * @throws Exception
	 */
	@SuppressWarnings("deprecation")
	@RequestMapping("exportTjrExcelMethod")
	public ModelAndView exportTjrExcelMethod(HttpServletRequest request, HttpServletResponse response) throws Exception{
		JSONObject params = new JSONObject();
		String queryType = RequestUtils.getStringParameter(request, "queryType");
		String sfzNum = RequestUtils.getStringParameter(request, "sfzNum");
		String realName = RequestUtils.getStringParameter(request, "realName");
		String user_name = RequestUtils.getStringParameter(request, "user_name");
		String sjhm = RequestUtils.getStringParameter(request, "sjhm");
		String tjr = RequestUtils.getStringParameter(request, "tjr");
		String regStartDate = RequestUtils.getStringParameter(request, "regStartDate");
		String regEndDate = RequestUtils.getStringParameter(request, "regEndDate");
		realName  = URLDecoder.decode(realName,"UTF-8");
		tjr  = URLDecoder.decode(tjr,"UTF-8");
		if(StringUtils.isNotEmpty(queryType) && "YWY_SSHK".equals(queryType)){//业务员所属客户查询
			SysUserDetails sysUserDetails =	(SysUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			tjr = sysUserDetails.getGyh();//业务员所属客户查询-当前登陆客户作为推荐人
		}
		
		params.put("sfzNum", sfzNum);
		params.put("realName", realName);
		System.out.println(realName);
		params.put("user_name", user_name);
		params.put("sjhm", sjhm);
		params.put("tjr", tjr);
		params.put("regStartDate", regStartDate);
		params.put("regEndDate", regEndDate);
		
		List list = tjrService.querytjr(params);
		List<String[]> dataList = com.bringway.p2p.constant.ExcelHeadConstant.getTjrExportSearchResultData(list);
		
		// 导出数据到Excel文件
		List<ExcelSheet> sheetList = new ArrayList<ExcelSheet>();
		ExcelSheet sheet = new ExcelSheet();
		sheet.setBodyList(dataList);
		sheet.setSheetName(DateUtils.getCurrentDataStr()+"推荐人信息");
		sheetList.add(sheet);
		OutputStream os = response.getOutputStream();
		String fileName = DateUtils.getCurrentDataStr() + "推荐人信息.xls";
		super.setDownLoadResponseHeader(request, response, fileName);
		response.setContentType("application/vnd.ms-excel");
		
		// 导出EXCEL
		ExcelOperUtil.writeExcel(sheetList, os);
		return null;
	}
	@RequestMapping("/queryTgcodeList")
	public ModelAndView queryTgcodeList(HttpServletRequest request,HttpServletResponse response) throws Exception{
		JSONObject result = new JSONObject();
		JSONObject params = new JSONObject();
		Integer start = RequestUtils.getIntParameter(request, "start");
		Integer limit = RequestUtils.getIntParameter(request, "limit");
		Integer page = RequestUtils.getIntParameter(request, "page");//新版本,传参
		if (null!=page) {//新版本,传参
			start=(page-1)*limit;
		}
		String startDate = RequestUtils.getStringParameter(request, "startDate");
		String endDate = RequestUtils.getStringParameter(request, "endDate");
		String username = RequestUtils.getStringParameter(request, "username");
		String sjhm = RequestUtils.getStringParameter(request, "sjhm");
		String tjr = RequestUtils.getStringParameter(request, "tjr");
		String tgCode = RequestUtils.getStringParameter(request, "tgCode");
		String tjjb = RequestUtils.getStringParameter(request, "tjjb");
		params.put("start", start);
		params.put("limit", limit);
		params.put("startDate", startDate);
		params.put("endDate", endDate);
		params.put("username", username);
		params.put("sjhm", sjhm);
		params.put("tjr", tjr);
		params.put("tgCode", tgCode);
		params.put("tjjb", tjjb);
		List list = tjrService.listUserTgInfo(params);
		int count = tjrService.getUserTgCount(params);
		result.put("data", list);	
		result.put("total", count);
		if (null!=page) {//新版本,传参
			int totalPage=count%limit==0?count/limit:count/limit+1;
			result.put("totalPage", totalPage);
		}
		serialize(request,response,result);
		return null;
	}
	@RequestMapping("/updateUserTggx")
	public ModelAndView updateUserTggx(HttpServletRequest request,HttpServletResponse response) throws Exception{
		JSONObject result = new JSONObject();
		boolean isSuc=tjrService.updateUserTggx();
		//result.put("success", true);
		result.put("isSuc", isSuc);
		serialize(request,response,result);
		return null;
	}

}
